﻿#region

using System.Collections.Generic;
using System.Linq;
using Styx;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;

#endregion

namespace Ink_Master.Bots.InkMaster.Actions
{
    public static class MountHelpers
    {
        public static Composite LandAndDismount()
        {
            Log.Slog("Entered LandAndDismount");
            LocalPlayer me = StyxWoW.Me;
            return new PrioritySelector(
                new Decorator(ret => me.Mounted && me.IsFlying,
                              new Sequence(
                                  new Action(ret => Log.Slog("Mounted and Flying")),
                                  new Action(ret => WoWMovement.MoveStop()),
                                  new Action(ret => Decend(me)),
                                  new Action(ret => Flightor.MountHelper.Dismount()))),
                new Decorator(ret => me.Mounted && !me.IsFlying,
                              new Sequence(
                                  new Action(ret => Log.Slog("Mounted but Not Flying")),
                                  new Action(ret => WoWMovement.MoveStop()),
                                  new Action(ret => Flightor.MountHelper.Dismount()))),
                new Decorator(ret => !me.Mounted && !me.IsFlying,
                              new Sequence(new Action(ret => Log.Slog("Not mounted or flying."))))
                );
        }

        private static void NavigateToPoint(WoWObject me, WoWPoint woWPoint)
        {
            while (woWPoint.Distance(me.Location) > 15f)
            {
                WoWMovement.ClickToMove(woWPoint);
            }
        }

        private static WoWPoint GetNearestUnit(IList<WoWUnit> woWUnit)
        {
            WoWUnit[] nearestunit = {woWUnit[0]};
            foreach (WoWUnit wUnit in woWUnit.Where(wUnit => wUnit.Distance < nearestunit[0].Distance))
            {
                nearestunit[0] = wUnit;
            }
            return nearestunit[0].Location;
        }

        private static WoWPoint GetNearestUnit(IList<WoWObject> woWObject)
        {
            WoWObject[] nearestunit = {woWObject[0]};
            foreach (WoWObject wUnit in woWObject.Where(wUnit => wUnit.Distance < nearestunit[0].Distance))
            {
                nearestunit[0] = wUnit;
            }
            return nearestunit[0].Location;
        }

        private static void Decend(WoWObject me)
        {
            ObjectManager.Update();
            List<WoWUnit> units = ObjectManager.GetObjectsOfType<WoWUnit>();
            List<WoWObject> objects = ObjectManager.GetObjectsOfType<WoWObject>();
            if (units.Any())
            {
                NavigateToPoint(me, GetNearestUnit(units));
            }
            else if (objects.Any())
            {
                NavigateToPoint(me, GetNearestUnit(objects));
            }
            else
            {
                Log.Slog("No units or objects found nearby for us to land near :(");
            }
        }
    }
}